package org.ielts.domain.log;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;

import org.ielts.domain.Mark;
import org.ielts.domain.base.Lesson;
import org.ielts.domain.base.SignCourse;

@Entity
@DiscriminatorValue("OrderLesson")
public class OrderLessonLog extends SchoolLog {
	public OrderLessonLog() {
		setLogType("OrderLesson");
	}

	@Override
	public String getLogType() {
		return "OrderLesson";
	}

	public String getTimespan() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
		return simpleDateFormat.format(getBeginTime()) + "-" + simpleDateFormat.format(getEndTime());
	}

	public boolean isTimeout() {
		return lesson.getBeginTime().before(this.getCreateTime());
	}

	private Date beginTime;

	private Date endTime;

	private Mark mark = Mark.None;

	private String teacherRemark;

	@ManyToOne(fetch = FetchType.EAGER)
	private Lesson lesson;

	@ManyToOne(fetch = FetchType.EAGER)
	private SignCourse signCourse;

	@Enumerated(EnumType.STRING)
	Status status;

	public enum Status {
		Ordered, Checked, Miss
	}

	private boolean beLate = false;

	private boolean leaveEarly = false;

	private boolean valid = true;

	public Lesson getLesson() {
		return lesson;
	}

	public void setLesson(Lesson lesson) {
		this.lesson = lesson;
		if (this.beginTime == null) {
			this.beginTime = lesson.getBeginTime();
			this.endTime = lesson.getEndTime();
		}
	}

	public SignCourse getSignCourse() {
		return signCourse;
	}

	public void setSignCourse(SignCourse signCourse) {
		this.signCourse = signCourse;
	}

	public boolean isBeLate() {
		return beLate;
	}

	public void setBeLate(boolean beLate) {
		this.beLate = beLate;
	}

	public boolean isLeaveEarly() {
		return leaveEarly;
	}

	public void setLeaveEarly(boolean leaveEarly) {
		this.leaveEarly = leaveEarly;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public Mark getMark() {
		return mark;
	}

	public void setMark(Mark mark) {
		this.mark = mark;
	}

	public String getTeacherRemark() {
		return teacherRemark;
	}

	public void setTeacherRemark(String teacherRemark) {
		this.teacherRemark = teacherRemark;
	}

	public Date getBeginTime() {
		if (lesson == null) {
			return beginTime;
		} else {
			return lesson.getBeginTime();
		}
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		if (lesson == null) {
			return endTime;
		} else {
			return lesson.getEndTime();
		}
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
}
